package com.example.qihuibao.widget.dialog

import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.qihuibao.R
import com.example.qihuibao.base.BaseDialogFragment
import com.example.qihuibao.bean.RspQueryFeesBean
import com.example.qihuibao.bean.RspQueryMealInfoBean
import com.example.qihuibao.constant.Config
import com.example.qihuibao.dao.MyDatabase
import com.example.qihuibao.utils.CommonUtil
import com.example.qihuibao.utils.extension.toast
import com.example.qihuibao.widget.view.CommonRecyclerView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 *@author: cww
 *@desc: 现场取餐（就餐）
 *@date: 5/16/23 10:58 AM
 */
class SceneMealPayDialog(val amount:String, val showPay:(amount:String)->Unit) : BaseDialogFragment(R.layout.dialog_scene_meal_pay) {
    private val keys = listOf("7", "8", "9", "4", "5", "6", "1", "2", "3", "0", ".", "×")
    private val sb = StringBuilder()

    private var tv_amount:TextView? = null

    override fun initView() {
        tv_amount = mView?.findViewById<TextView>(R.id.tv_amount)
        tv_amount?.text = "金额：\n￥$amount"
        mView?.findViewById<Button>(R.id.btn_cancel)?.setOnClickListener {
            dismiss()
//            click(false)
        }
        mView?.findViewById<Button>(R.id.btn_ok)?.setOnClickListener {
            if (CommonUtil.inputIllegal(sb.toString())) {
                activity?.toast("输入有误，请重新输入")
            }else {
                mView?.findViewById<TextView>(R.id.tv_pay_status)?.text = "等待支付..."
                showPay(if (sb.isEmpty()) amount else sb.toString())
            }
        }
        mView?.findViewById<CommonRecyclerView>(R.id.rv_keyboard)?.init(
            R.layout.item_key,
            GridLayoutManager(context, 3),
            keys,
            {parent, pos ->
                (parent as Button).text = keys[pos]
            },
            {
                if (keys[it] != "×") {
                    sb.append(keys[it])
                }else if (sb.isNotEmpty()) {
                    sb.setLength(sb.length-1)
                }
                tv_amount?.text = "金额：\n￥$sb"
            }
        )
    }

}